package com.qiyu.tech.gateway.auth;

import com.qiyu.tech.gateway.dao.mapper.BlackListMapper;
import com.qiyu.tech.gateway.dao.po.BlackListPO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地缓存
 *
 * @Author linhao
 * @Date created in 9:53 下午 2021/3/16
 */
@Service
public class BlackListCache implements IBlackListCache,InitializingBean {

    @Resource
    private BlackListMapper blackListMapper;

    private static ConcurrentHashMap<String, BlackListPO> ipBlackMap = new ConcurrentHashMap<>(16);
    private static ConcurrentHashMap<String, BlackListPO> userIdBlackMap = new ConcurrentHashMap<>(16);
    private static ConcurrentHashMap<String, BlackListPO> uaBlackMap = new ConcurrentHashMap<>(16);

    private static List<BlackListPO> blackListPOS = null;

    private volatile boolean hasReadData = false;

    @Override
    public void afterPropertiesSet()  {
        blackListPOS = blackListMapper.selectAll();
        for (BlackListPO blackListPO : blackListPOS) {
            int type = blackListPO.getType();
            if (type == 0) {
                this.setUserIdBlackMap(blackListPO.getIdentification(),blackListPO);
            } else if (type == 1) {
                this.setIpBlackMap(blackListPO.getIdentification(),blackListPO);
            } else if (type == 2) {
                this.setUaBlackMap(blackListPO.getIdentification(),blackListPO);
            }
        }
        this.hasReadData = true;
        System.out.println("@@@@@@ init cache success @@@@@@");
    }


    @Override
    public Map<String, BlackListPO> getIpBlackMap() {
        return ipBlackMap;
    }

    @Override
    public Map<String, BlackListPO> getUserIdBlackMap() {
        return userIdBlackMap;
    }

    @Override
    public Map<String, BlackListPO> getUaBlackMap() {
        return uaBlackMap;
    }

    @Override
    public BlackListPO getFromIpBlackMap(String key) {
        return ipBlackMap.get(key);
    }

    @Override
    public BlackListPO getFromUserIdBlackMap(String key) {
        return userIdBlackMap.get(key);
    }

    @Override
    public BlackListPO getFromUaBlackMap(String key) {
        return uaBlackMap.get(key);
    }

    @Override
    public void setIpBlackMap(String key, BlackListPO blackListPO) {
        ipBlackMap.put(key, blackListPO);
    }

    @Override
    public void setUserIdBlackMap(String key, BlackListPO blackListPO) {
        userIdBlackMap.put(key, blackListPO);
    }

    @Override
    public void setUaBlackMap(String key, BlackListPO blackListPO) {
        uaBlackMap.put(key, blackListPO);
    }

    @Override
    public void setInCacheMap(String key, BlackListPO blackListPO) {
        int type = blackListPO.getType();
        if (type == 0) {
            this.setUserIdBlackMap(blackListPO.getIdentification(),blackListPO);
        } else if (type == 1) {
            this.setIpBlackMap(blackListPO.getIdentification(),blackListPO);
        } else if (type == 2) {
            this.setUaBlackMap(blackListPO.getIdentification(),blackListPO);
        }
    }
}
